﻿using System;
using System.IO;
using System.Net;

namespace UnionnetClient.Model
{
	/// <summary>
	///维护内部状态切换
	/// </summary>
	public class StateMachine
	{
		internal StateMachine(IStateFactory stateFactory)
		{
			this.stateFactory = stateFactory;
		}

		internal event EventHandler StateChanged;

		internal StateBase CurrentState
		{
			get { return currentState; }
		}

		#region State

		internal UnconfigState UnconfigS
		{
			get
			{
				if (unconfigState == null)
				{
					unconfigState = stateFactory.CreateUncofingState(this);
				}
				return unconfigState;
			}
		}

		internal UnloginState UnloginS
		{
			get
			{
				if (unloginState == null)
				{
					unloginState = stateFactory.CreateUnloginState(this);
				}
				return unloginState;
			}
		}

		internal VpnState VpnS
		{
			get
			{
				if (vpnState == null)
				{
					vpnState = stateFactory.CreateVpnState(this);
				}
				return vpnState;
			}
		}

		internal NonVpnState NonVpnS
		{
			get
			{
				if (nonVpnState == null)
				{
					nonVpnState = stateFactory.CreateNonVpnState(this);
				}
				return nonVpnState;
			}
		}

		internal UnknownLoginedState UnknownLoginedS
		{
			get
			{
				if (unknownLoginedState == null)
				{
					unknownLoginedState = stateFactory.CreateUnknownLoginedState(this);
				}
				return unknownLoginedState;
			}
		}

		#endregion

		internal void SetState(StateBase currentState)
		{
			this.currentState = currentState;
			OnStateChanged();
		}

		private StateBase currentState;
		private IStateFactory stateFactory;

		private UnconfigState unconfigState;
		private UnloginState unloginState;
		private VpnState vpnState;
		private NonVpnState nonVpnState;
		private UnknownLoginedState unknownLoginedState;

		private void OnStateChanged()
		{
			if (StateChanged != null)
			{
				StateChanged(null, EventArgs.Empty);
			}
		}
	}
}